package com.xjrsoft.module.language.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageInput;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.base.entity.*;
import com.xjrsoft.module.base.service.*;
import com.xjrsoft.module.language.dto.LgMarkDto;
import com.xjrsoft.module.language.entity.XjrLgMark;
import com.xjrsoft.module.language.service.IXjrLgMarkService;
import com.xjrsoft.module.language.vo.LgMarkVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zlf
 * @since 2021-08-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/lg-mark")
@Api(value = "/lg-mark", tags = "翻译标记管理")
public class XjrLgMarkController {

    private final IXjrLgMarkService lgMarkService;

    private final IXjrBaseModuleService moduleService;

    private final IXjrBaseModuleButtonService moduleButtonService;

    private final IXjrBaseModuleColumnService moduleColumnService;

    private final IXjrBaseModuleFormService moduleFormService;

    private final IXjrBaseDataItemDetailService dataItemDetailService;

    @GetMapping
    @ApiOperation(value = "获翻译标记列表")
    public Response<List<LgMarkVo>> getList(@RequestParam(required = false) String keyword) {
        if (StringUtil.isEmpty(keyword)) {
            return Response.ok();
        }
        List<XjrLgMark> lgMarkList = lgMarkService.list(Wrappers.<XjrLgMark>query().lambda().or(!StringUtil.isEmpty(keyword),
                wrapper -> wrapper.like(XjrLgMark::getName, keyword).or().like(XjrLgMark::getEnCode, keyword)));
        return Response.ok(BeanUtil.copyList(lgMarkList, LgMarkVo.class));
    }

    @GetMapping("/page")
    @ApiOperation(value = "获翻译标记列表，分页")
    public Response<PageOutput<LgMarkVo>> getTreeList(PageInput dto) {
        String keyword = dto.getKeyword();
        IPage<XjrLgMark> page = lgMarkService.page(ConventPage.getPage(dto), Wrappers.<XjrLgMark>query().lambda().or(!StringUtil.isEmpty(keyword),
                wrapper -> wrapper.like(XjrLgMark::getName, keyword).or().like(XjrLgMark::getEnCode, keyword)));
        return Response.ok(ConventPage.getPageOutput(page, LgMarkVo.class));
    }

    @PostMapping
    @ApiOperation(value = "新增翻译标记")
    public Response save(@RequestBody LgMarkDto lgMarkDto) {
        // 唯一校验
        String enCode = lgMarkDto.getEnCode();
        if (!lgMarkService.isUnique(null, enCode)) {
            return Response.notOk("翻译标记编码(" + enCode + ")已存在！");
        }

        XjrLgMark lgMark = BeanUtil.copy(lgMarkDto, XjrLgMark.class);
        if (StringUtil.isEmpty(lgMark.getParentId())) {
            lgMark.setParentId(StringPool.ZERO);
        }
        return Response.status(lgMarkService.save(lgMark));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改翻译标记")
    @Transactional
    public Response update(@PathVariable String id, @RequestBody LgMarkDto lgMarkDto) {
        // 唯一校验
        String enCode = lgMarkDto.getEnCode();
        if (!lgMarkService.isUnique(id, enCode)) {
            return Response.notOk("翻译标记编码(" + enCode + ")已存在！");
        }
        XjrLgMark oldLgMark = lgMarkService.getById(id);
        XjrLgMark lgMark = BeanUtil.copy(lgMarkDto, XjrLgMark.class);
        if (StringUtil.isEmpty(lgMark.getParentId())) {
            lgMark.setParentId(StringPool.ZERO);
        }
        lgMark.setId(id);

        if (StringUtil.equalsIgnoreCase(oldLgMark.getEnCode(), lgMarkDto.getEnCode())) {
            // 更新绑定的菜单
            List<XjrBaseModule> moduleList = moduleService.list(Wrappers.<XjrBaseModule>query().lambda().eq(XjrBaseModule::getLgMarkCode, oldLgMark.getEnCode()));
            if (CollectionUtil.isNotEmpty(moduleList)) {
                List<XjrBaseModule> updatedModuleList = new ArrayList<>(moduleList.size());
                for (XjrBaseModule module : moduleList) {
                    XjrBaseModule updatedModule = new XjrBaseModule();
                    updatedModule.setModuleId(module.getModuleId());
                    updatedModule.setLgMarkCode(lgMark.getEnCode());
                    updatedModuleList.add(updatedModule);
                }
                moduleService.updateBatchById(updatedModuleList);
            }
            // 更新绑定按钮
            List<XjrBaseModuleColumn> moduleColumnList = moduleColumnService.list(Wrappers.<XjrBaseModuleColumn>query().lambda().eq(XjrBaseModuleColumn::getLgMarkCode, oldLgMark.getEnCode()));
            if (CollectionUtil.isNotEmpty(moduleColumnList)) {
                List<XjrBaseModuleColumn> updatedModuleColumnList = new ArrayList<>(moduleColumnList.size());
                for (XjrBaseModuleColumn moduleColumn : moduleColumnList) {
                    XjrBaseModuleColumn updatedModuleColumn = new XjrBaseModuleColumn();
                    updatedModuleColumn.setModuleColumnId(moduleColumn.getModuleColumnId());
                    updatedModuleColumn.setLgMarkCode(lgMark.getEnCode());
                    updatedModuleColumnList.add(updatedModuleColumn);
                }
                moduleColumnService.updateBatchById(updatedModuleColumnList);
            }
            // 更新绑定列表字段
            List<XjrBaseModuleButton> moduleButtonList = moduleButtonService.list(Wrappers.<XjrBaseModuleButton>query().lambda().eq(XjrBaseModuleButton::getLgMarkCode, oldLgMark.getEnCode()));
            if (CollectionUtil.isNotEmpty(moduleButtonList)) {
                List<XjrBaseModuleButton> updatedModuleButtonList = new ArrayList<>(moduleButtonList.size());
                for (XjrBaseModuleButton moduleButton : moduleButtonList) {
                    XjrBaseModuleButton updatedModuleButton = new XjrBaseModuleButton();
                    updatedModuleButton.setModuleButtonId(moduleButton.getModuleButtonId());
                    updatedModuleButton.setLgMarkCode(lgMark.getEnCode());
                    updatedModuleButtonList.add(updatedModuleButton);
                }
                moduleButtonService.updateBatchById(updatedModuleButtonList);
            }
            // 更新绑定表单字段
            List<XjrBaseModuleForm> moduleFormList = moduleFormService.list(Wrappers.<XjrBaseModuleForm>query().lambda().eq(XjrBaseModuleForm::getLgMarkCode, oldLgMark.getEnCode()));
            if (CollectionUtil.isNotEmpty(moduleFormList)) {
                List<XjrBaseModuleForm> updatedModuleFormList = new ArrayList<>(moduleFormList.size());
                for (XjrBaseModuleForm moduleForm : moduleFormList) {
                    XjrBaseModuleForm updatedModuleForm = new XjrBaseModuleForm();
                    updatedModuleForm.setModuleFormId(moduleForm.getModuleFormId());
                    updatedModuleForm.setLgMarkCode(lgMark.getEnCode());
                    updatedModuleFormList.add(updatedModuleForm);
                }
                moduleFormService.updateBatchById(updatedModuleFormList);
            }
            // 更新绑定数据字典
            List<XjrBaseDataItemDetail> dataItemDetailList = dataItemDetailService.list(Wrappers.<XjrBaseDataItemDetail>query().lambda().eq(XjrBaseDataItemDetail::getLgMarkCode, oldLgMark.getEnCode()));
            if (CollectionUtil.isNotEmpty(dataItemDetailList)) {
                List<XjrBaseDataItemDetail> updatedDataItemDetailList = new ArrayList<>(moduleList.size());
                for (XjrBaseDataItemDetail dataItemDetail : dataItemDetailList) {
                    XjrBaseDataItemDetail updatedDataItemDetail = new XjrBaseDataItemDetail();
                    updatedDataItemDetail.setItemDetailId(dataItemDetail.getItemDetailId());
                    updatedDataItemDetail.setLgMarkCode(lgMark.getEnCode());
                    updatedDataItemDetailList.add(updatedDataItemDetail);
                }
                dataItemDetailService.updateBatchById(updatedDataItemDetailList);
            }
        }
        return Response.status(lgMarkService.updateById(lgMark));
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除翻译标记")
    public Response delete(@PathVariable String ids) {
        String[] idArray = StringUtils.split(ids, StringPool.COMMA);
        boolean isSuccess = false;
        if (idArray.length > 1) {
            isSuccess = lgMarkService.removeByIds(Arrays.asList(idArray));
        } else {
            isSuccess = lgMarkService.removeById(idArray[0]);
        }
        return Response.status(isSuccess);
    }
}
